package com.lyc.kotlin.wanandroid.receiver

import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import com.lyc.kotlin.wanandroid.constant.Constant
import com.lyc.kotlin.wanandroid.event.NetworkChangeEvent
import com.lyc.kotlin.wanandroid.utils.NetWorkUtil
import com.lyc.kotlin.wanandroid.utils.Preference
import org.greenrobot.eventbus.EventBus

/**
 * 描述
 *liuyucheng
 * Created By 刘育成 on 2019/7/30.
 */
class NetworkChangeReceiver :BroadcastReceiver(){
    /**
     * 缓存上一次的网络状态
     */
    private var hasNetwork: Boolean by Preference(Constant.HAS_NETWORK_KEY, true)
    override fun onReceive(context: Context, intent: Intent) {
        val isConnected=NetWorkUtil.isNetworkConnected(context)
        if (isConnected){
            if (isConnected!=hasNetwork){
                EventBus.getDefault().post(NetworkChangeEvent(isConnected))
            }
        }else{
            EventBus.getDefault().post(NetworkChangeEvent(isConnected))
        }
    }
}